import 'dart:convert';
import 'dart:developer';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter/src/rendering/object.dart';
import 'dart:io';

import 'package:toast/toast.dart';


class BottomNavigationWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new BottomNavigationWidgetState();
  }
}

class BottomNavigationWidgetState extends State<BottomNavigationWidget> {
  int _currentIndex = 0;
  // static String body = "";
  static String text = "page info tap:0 :";
  late Scaffold scaf;
  static Center returnCenter(body) {
    return new Center(
      child: new Text(body+text),
    );
  }
  final List<Widget> _children = [
    // returnCenter(text),
    // returnCenter(text),
    // returnCenter(text),
    Center(
      child: Text('${text}'),
    ),
    Center(
      child: Text('${text}'),
    ),
    Center(
      child: Text('${text}'),
    ),
  ];
  Future<void> req_text() async {
    var page = _currentIndex+1;
    Toast.show(
        "test1", //必填
        context, //必填
        duration: Toast.LENGTH_SHORT,
        gravity:  Toast.CENTER,
        backgroundRadius:4
    );
    var httpClient = new HttpClient();
    Toast.show(
        "test2", //必填
        context, //必填
        duration: Toast.LENGTH_SHORT,
        gravity:  Toast.CENTER,
        backgroundRadius:4
    );
    var uri = new Uri.http('idea.developer.hn.cn', '/app/forum/list', {'title': '', 'page': page.toString(),'count':"2","circleId":"0","ajax":"1"});
    var request = await httpClient.getUrl(uri);
    Toast.show(
        "test2", //必填
        context, //必填
        duration: Toast.LENGTH_SHORT,
        gravity:  Toast.CENTER,
        backgroundRadius:4
    );
    var response = await request.close();
    // await response.transform(Utf8Decoder()).join().then((value) =>
    //   setState(() {
    //     text = "page info tap:test";
    //     // text = "page info tap:"+_currentIndex.toString()+" :"+value;
    //     Toast.show(
    //         text, //必填
    //         context, //必填
    //         duration: Toast.LENGTH_SHORT,
    //         gravity:  Toast.CENTER,
    //         backgroundRadius:4
    //     );
    //   })
    // ).catchError((value) =>{
    //   Toast.show(
    //     "请求失败", //必填
    //     context, //必填
    //     duration: Toast.LENGTH_SHORT,
    //     gravity:  Toast.CENTER,
    //     backgroundRadius:4
    //   )
    // });
    String value = await response.transform(Utf8Decoder()).join();
    setState(() {
      // text = "page info tap:test";
      text = "page info tap:"+_currentIndex.toString()+" :"+value;
      Toast.show(
          text, //必填
          context, //必填
          duration: Toast.LENGTH_SHORT,
          gravity:  Toast.CENTER,
          backgroundRadius:4
      );
    });
  }

  @override
  Widget build(BuildContext context) {
    /*
    返回一个脚手架，里面包含两个属性，一个是底部导航栏，另一个就是主体内容
     */
    scaf = new Scaffold(

      // createState:_children[_currentIndex],

      appBar: new AppBar(
        title: new Text("应用栏标题")
      ),
      // body: _children[_currentIndex],
      body: Text(text),
      bottomNavigationBar: BottomNavigationBar(
        //底部导航栏的创建需要对应的功能标签作为子项，这里我就写了3个，每个子项包含一个图标和一个title。
        items: [
          BottomNavigationBarItem(
              icon: Icon(
                Icons.home,
              ),
              title: new Text(
                '首页',
              )),
          BottomNavigationBarItem(
              icon: Icon(
                Icons.business,
              ),
              title: new Text(
                '商业',
              )),
          BottomNavigationBarItem(
              icon: Icon(
                Icons.mood,
              ),
              title: new Text(
                '我的',
              )),
        ],
        //这是底部导航栏自带的位标属性，表示底部导航栏当前处于哪个导航标签。给他一个初始值0，也就是默认第一个标签页面。
        currentIndex: _currentIndex,
        //这是点击属性，会执行带有一个int值的回调函数，这个int值是系统自动返回的你点击的那个标签的位标
        onTap: (int i) {
          Toast.show(
              "测试啊", //必填
              context, //必填
              duration: Toast.LENGTH_SHORT,
              gravity:  Toast.CENTER,
              backgroundRadius:4
          );
          //进行状态更新，将系统返回的你点击的标签位标赋予当前位标属性，告诉系统当前要显示的导航标签被用户改变了。
          setState(() {
            _currentIndex = i;
          });
          req_text();
        },
      ),
    );
    return scaf;
  }
}


/**
 * 应用app
 */
class MyApp extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new BottomNavigationWidget(),
    );
  }
}

/**
 * 初始化加载函数
 */
void main() {
  runApp(MyApp());
}